package com.auto.survey.activity.monitoring.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.entity.MonitoringPoint
import java.util.Collections

class CtrlAddManagerAdapter() : RecyclerView.Adapter<CtrlAddManagerAdapter.CtrlAddViewHodler>(){

    private var mItems = mutableListOf<MonitoringPoint>()
    private var mListener:OnItemClickListener?=null

    // 保存 ItemTouchHelper 实例（需外部传入）
    private var itemTouchHelper: ItemTouchHelper? = null

    private var mDelteListner:onItemDeleteClickListener?=null

    // 记录当前长按/拖动的位置
    private var dragPosition = -1

    fun setItemTouchHeler(touchHelper: ItemTouchHelper) {
        itemTouchHelper = touchHelper
    }

    fun setData(items:List<MonitoringPoint>) {
        mItems.clear()
        mItems.addAll(items)
        notifyDataSetChanged()
    }


    fun setItemClickListener(listener: OnItemClickListener){
        mListener = listener
    }

    fun setDeleItemClickListener (listener:onItemDeleteClickListener) {
        mDelteListner = listener
    }

    // 交换两个位置的数据
    fun swapItems(fromPos: Int, toPos: Int) {
        Collections.swap(mItems, fromPos, toPos) // 直接交换网格数据
    }

    // 重置拖动状态
    fun resetDragState() {
        val oldPosition = dragPosition
        dragPosition = -1
        if (oldPosition != -1) {
            notifyItemChanged(oldPosition) // 刷新之前的 Item
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CtrlAddViewHodler {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_ctrl_add_manager, parent, false)
        return CtrlAddViewHodler(view)
    }

    override fun onBindViewHolder(holder: CtrlAddViewHodler, position: Int) {
        holder.tv_item_ctrladd.text = mItems[position].pointName
        if (mItems[position].isWarning) {
            holder.tv_item_ctrladd.setBackgroundResource(R.drawable.item_ctrladd_choiced_bg)
        } else {
            holder.tv_item_ctrladd.setBackgroundResource(R.drawable.item_ctrladd_unchoiced_bg)
        }

        holder.iv_delete.visibility = View.GONE

        holder.tv_item_ctrladd.setOnClickListener {
            mListener?.let {
                holder.tv_item_ctrladd.setBackgroundResource(R.drawable.item_ctrladd_choiced_bg)
                mItems[position].collected = true
                it.onItemClick(mItems[position], position)
            }
        }

        holder.tv_item_ctrladd.setOnLongClickListener {
            itemTouchHelper?.let {
                holder.iv_delete.visibility = View.VISIBLE
                it.startDrag(holder)
            }

            true
        }

        holder.iv_delete.setOnClickListener {
            // 关键修复：获取当前有效位置
            val currentPosition = holder.adapterPosition
            if (currentPosition == RecyclerView.NO_POSITION) return@setOnClickListener

            mDelteListner?.onDelteItemClick(mItems[currentPosition], currentPosition)
            mItems.removeAt(currentPosition)
            notifyItemRemoved(currentPosition)
            // 通知后续项位置变化
            notifyItemRangeChanged(currentPosition, mItems.size - currentPosition)
        }
    }



    override fun getItemCount(): Int {
        if (mItems.isNotEmpty()) {
            return mItems.size
        }
        return 0
    }

    inner class CtrlAddViewHodler(itemView: View): RecyclerView.ViewHolder(itemView) {
        val iv_delete = itemView.findViewById<ImageView>(R.id.iv_itemview_ctrl_delete)
        val tv_item_ctrladd = itemView.findViewById<TextView>(R.id.tv_item_ctrladd)
    }

    interface OnItemClickListener {
        fun onItemClick(point:MonitoringPoint, position: Int)
    }

    interface onItemDeleteClickListener {
        fun onDelteItemClick(point:MonitoringPoint, position: Int)
    }
}